<template>
  <div class="edit-container">
    <!-- 顶部栏 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" />
    <!-- 头像 -->
    <div class="photo">
      <van-image
        width="120"
        height="120"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 编辑框 -->
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="editNameClick"
      />
    </van-cell-group>
    <van-cell-group>
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showGender = true"
      />
    </van-cell-group>
    <van-cell-group>
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="
          showBirthday = true
          currentDate = new Date(userInfo.birthday)
        "
      />
    </van-cell-group>
    <!-- 姓名输入框 -->
    <van-dialog
      v-model="showName"
      title="编辑姓名"
      show-cancel-button
      @confirm="updatasName"
    >
      <van-field ref="nameInput" v-model="nameValue" placeholder="请输入性名" />
    </van-dialog>
    <!-- 性别输入框 -->
    <van-popup v-model="showGender" position="bottom">
      <van-nav-bar title="编辑性别" left-arrow />
      <van-cell title="男" is-link @click="editGenderClick(0)" />
      <van-cell title="女" is-link @click="editGenderClick(1)" />
    </van-popup>
    <!-- 生日编辑框 -->
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showBirthday = false"
        @confirm="updatesBirthday"
      />
    </van-popup>
    <!-- 头像裁剪 -->
    <div class="cropper" v-show="showPhoto">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        centerBox
        canScale
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button type="primary" @click="showPhoto = false">取消</van-button>
      <van-button type="info" @click="editPhotoClick">确认</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo } from './user.js'
import { editUserPhoto } from './user.js'
import * as moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      showName: false,
      nameValue: '',
      showGender: false,
      showBirthday: false,
      showPhoto: false,
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      img: ''
    }
  },
  methods: {
    // 姓名编辑
    editNameClick () {
      this.showName = true
      this.nameValue = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.nameInput.focus()
      })
    },
    // 修改姓名
    async updatasName () {
      await editUserInfo({ name: this.nameValue })
      this.$store.commit('updates', {
        ...this.userInfo,
        name: this.nameValue
      })
      this.$toast.success('修改成功')
    },
    // 编辑性别
    async editGenderClick (data) {
      this.showGender = false
      await editUserInfo({ gender: data })
      this.$store.commit('updates', {
        ...this.userInfo,
        gender: data
      })
      this.$toast.success('修改成功')
    },
    // 编辑生日
    async updatesBirthday (value) {
      let birthday = moment(value).format('YYYY-MM-DD')
      this.showBirthday = false
      await editUserInfo({ birthday })
      this.$store.commit('updates', {
        ...this.userInfo,
        birthday
      })
      this.$toast.success('修改成功')
    },
    // 头像裁剪
    afterRead (file) {
      this.img = file.content;
      this.showPhoto = true;
    },
    // 编辑头像
    editPhotoClick () {
      this.$refs.cropper.getCropData(async data => {
        let fd = new FormData()
        fd.append('photo', data)
        await editUserPhoto(fd)
        this.$store.commit('updates', {
          ...this.userInfo,
          photo: data,
        })
        this.showPhoto = false;
        this.$toast.success('修改成功')
      })
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  components: {
    VueCropper
  }
}
</script>

<style lang="less">
.edit-container {
  .van-nav-bar {
    background-color: #0098fa;
    .van-nav-bar__title,
    .van-icon {
      color: #fff;
    }
  }
  .photo {
    text-align: center;
    margin: 25px 0;
    .van-uploader {
      position: fixed;
      opacity: 0;
      left: 51%;
      transform: translateX(-50%);
      height: 120px;
      width: 130px;
      .van-uploader__upload {
        height: 120px;
        width: 130px;
      }
    }
  }
  .cropper {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
  }
  .van-button--primary {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .van-button--info {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
